Skip to content

feat(agent): add Codex agent wrapper and ReMe MCP bridge#358

Merged
ployts merged 6 commits into
agentscope-ai:mainfrom
jinliyl:dev/codex
Jul 17, 2026
Merged

feat(agent): add Codex agent wrapper and ReMe MCP bridge#358
ployts merged 6 commits into
agentscope-ai:mainfrom
jinliyl:dev/codex

Conversation

@jinliyl

@jinliyl jinliyl commented Jul 14, 2026

Copy link
Copy Markdown
Member

变更概述

完成 #352
本 PR 为 ReMe 的统一 Agent Wrapper 增加 Codex 后端,使现有 Job 可以通过 Codex 执行,并将 ReMe Job 以 MCP 工具的形式注入 Codex 线程。同时补充 API Key 与 OAuth 两种默认配置,保持与 AgentScope、Claude Code 后端一致的调用和流式响应接口。

主要改动

1. 新增 Codex Agent Wrapper

  • 新增 CodexAgentWrapper 并注册 codex backend。
  • 基于 openai-codex Python SDK 实现同步 reply 与流式 reply_stream
  • 支持新建、恢复和 fork Codex thread,并统一使用 thread id 作为 ReMe session id。
  • 支持 model、approval mode、sandbox、reasoning effort、summary、service tier、structured output 等 Codex 参数。
  • 支持从显式参数、Codex/OpenAI/LLM 环境变量及 ReMe 默认 LLM credential 中按优先级解析 API Key 与 Base URL。
  • 支持独立的 CODEX_HOME,默认将会话状态保存在 ReMe workspace 的 mem_session/codex;OAuth 配置可直接使用用户的 ~/.codex
  • 当启用全部项目 skills 时,将 ReMe 项目 skills 目录链接到 Codex home,避免复制和维护重复内容。

2. 新增 ReMe Job MCP 桥接

  • 新增基于 FastMCP STDIO 的桥接服务,将选中的普通 ReMe Job 注册为 Codex MCP tools。
  • 保留 Job 原有的 description 与 JSON Schema parameters。
  • 支持注入由 wrapper 管理的 tool_context_id,并阻止调用方覆盖该上下文。
  • 显式拒绝 BackgroundJob 和 StreamJob,避免将不适合 request/response 工具协议的 Job 暴露给 Codex。
  • MCP 子进程具有独立的 ReMe Application 生命周期,启动和关闭时会正确初始化、释放资源。
  • 子进程会过滤 background/cron jobs,避免短生命周期工具服务启动 workspace watcher 或定时任务。

3. 统一流式事件映射

  • 将 Codex app-server 的 turn、message、reasoning、plan、command、file change、MCP tool、usage 和 error 通知映射为现有 StreamChunk / ChunkEnum
  • 保留 thread id、turn id、item id、tool call id、token usage、状态和执行耗时等信息。
  • 更新相关 schema 文档,明确统一流协议现在覆盖 AgentScope、Claude Code 与 Codex。

4. 默认配置与依赖

  • default.yaml 中新增:
    • codex:适用于 API Key / 自定义 Base URL。
    • codex_oauth:复用 ~/.codex 登录状态。
  • openai-codex>=0.1.0b3 加入 core 依赖。
  • 清理未被代码或配置引用的 benchmark optional extra 及其 portalocker 依赖。

兼容性说明

  • 现有 AgentScope 与 Claude Code wrapper 的公开接口和默认配置保持不变。
  • Codex 当前仅支持字符串输入。
  • skills 当前支持 skills="all";单独选择部分 skill 会明确报错,避免静默产生不完整配置。
  • structured output 会解析最终响应 JSON;返回非法 JSON 时会给出明确错误。

测试与检查

新增单元测试覆盖:

  • MCP server 配置及已有 MCP servers 合并。
  • Job 选择、参数 schema 与 tool context 注入。
  • BackgroundJob 拒绝逻辑。
  • FastMCP STDIO 子进程启动和工具发现。
  • Codex 流式内容、usage 与 turn completion 映射。
  • 非流式响应、thread id 与 structured output。
  • skills 限制、Codex home 路径展开及 OAuth 默认配置。

本地验证结果:

  • PYTHONPATH=. pytest tests/unit/test_codex_agent_wrapper.py -v:13 passed。
  • 相关文件执行 pre-commit:AST、YAML、TOML、私钥检测、尾随空格、trailing commas、Black、Flake8、Pylint、Pyroma 全部通过。
  • git diff --check:通过。

@jinliyl
jinliyl requested a review from ployts July 14, 2026 15:09
jinliyl and others added 5 commits July 17, 2026 11:33
…e testing

- Implement structured output schema normalization across all wrappers
- Add Claude Code system prompt mode support with append/replace options
- Introduce Codex agent wrapper with streaming, tool context isolation, and skill management
- Enhance skill linking with validation and conflict resolution
- Add approval event streaming support for Codex wrapper
- Implement output schema validation and normalize function
- Create dedicated test suites for Claude Code and Codex integration
- Update README documentation for Codex wrapper capabilities
- Refactor kwargs merging with proper schema handling
- Add tool context validation when resuming sessions
- Implement proper cleanup and session management for Codex wrapper
- Add docstring for empty schema validation in build_options
- Document falsy structured output preservation behavior
- Add docstring for streaming wrapper schema rejection
- Include lambda function reference for wrapper factory consistency
- Add test documentation for live Codex wrapper contract exercise

@ployts ployts left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@ployts
ployts merged commit c1a25e9 into agentscope-ai:main Jul 17, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants